<?php

namespace App\Http\Model;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;
use App\Http\Model\AudioApi;


class NationRecharge extends Base
{
    protected $table = 'nation_recharge_bag';

    public function __construct(array $attributes = [])
    {
        parent::__construct($this->table, $attributes);
    }

    //查看等级礼包
    public function getLogToRank($user_id,$rank)
    {
        $where['user_id'] = $user_id;
        $where['rank'] = $rank;
        $date[] = date('Y-m-d 00:00:00', time());
        $date[] = date('Y-m-d 23:59:59', time());
        return DB::table($this->table)->where($where)->whereBetween('created_at',$date)->first();
    }

    public function createLog($user_id,$rank)
    {
        $data['user_id'] = $user_id;
        $data['rank'] = $rank;
        return DB::table($this->table)->insertGetId($data);
    }

    public function getLogByUserId($user_id,$feild = ['*'])
    {

        $date[] = date('Y-m-d 00:00:00', time());
        $date[] = date('Y-m-d 23:59:59', time());
        $res = DB::table($this->table)->where(['user_id'=>$user_id])->whereBetween('created_at',$date)->select($feild)->orderBy('rank','asc')->get();
        return $this->objToArray($res);
    }

    public function updateLog($user_id,$rank,$gift_log = '')
    {
        $where['user_id'] = $user_id;
        $where['rank'] = $rank;
        $date[] = date('Y-m-d 00:00:00', time());
        $date[] = date('Y-m-d 23:59:59', time());
        return DB::table($this->table)->where($where)->whereBetween('created_at',$date)->update(['status'=>1,'gift_log'=>$gift_log]);
    }

    public function scoreToUser()
    {
        $sql = "select g.user_id,IFNULL(score_num,0) as score from hd_nation_grid_log as g left join (select user_id,sum(gift_value) as score_num from hd_nation_mall_log where gift_type = 2 or gift_type = 10 group by user_id) m on g.user_id = m.user_id;";
        $data = DB::select($sql);
        $data = $this->objToArray($data);
        return DB::table('score_to_user')->insert($data);
    }

    public function getUserScoreLog()
    {
        $where['status'] = 0;
        $res = DB::table('score_to_user')->where($where)->get();
        return $this->objToArray($res);
    }

    public function updateUserScoreLog($id)
    {
        $where['id'] = $id;
        $data['status'] = 1;
        $res = DB::table('score_to_user')->where($where)->update($data);
        return $this->objToArray($res);
    }

}